package com.ql.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ql.dao.HomeWorkSettingDao;
import com.ql.dao.SectionDao;
import com.ql.domain.HomeWorkSetting;
import com.ql.domain.Section;
import com.ql.domain.response.JsonResult;
import com.ql.service.HomeWorkSettingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class HomeWorkSettingServiceImpl implements HomeWorkSettingService {
    @Autowired
    HomeWorkSettingDao homeWorkSettingDao;
    @Autowired
    SectionDao sectionDao;

    @Override
    public JsonResult findAll(String userId, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Section> sectionList = sectionDao.findByUserId(userId);
        List<Integer> sectionIds = new ArrayList<>();
        for (Section section : sectionList) {
            sectionIds.add(section.getId());
        }
        List<HomeWorkSetting> workSettingList = homeWorkSettingDao.findAll(sectionIds);
        PageInfo pageInfo = new PageInfo(workSettingList);
        return JsonResult.success(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public JsonResult save(HomeWorkSetting homeWorkSetting, String userId) {
        //获取标识
        String logo = homeWorkSetting.getLogo();
        HomeWorkSetting workSetting = homeWorkSettingDao.findOneByLogo(logo);
        //获取标段id
        Integer sectionId = homeWorkSetting.getSectionId();
        if (sectionId != null) {
            if (workSetting == null) {
                //添加
                homeWorkSettingDao.add(homeWorkSetting);
                return JsonResult.success("添加成功");
            } else {
                //更新
                homeWorkSettingDao.update(homeWorkSetting);
                return JsonResult.success("更新成功");
            }
        } else {
            List<Section> sectionList = sectionDao.findByUserId(userId);
            for (Section section : sectionList) {
                Integer id = section.getId();
                homeWorkSetting.setSectionId(id);
                if (workSetting == null) {
                    //添加
                    homeWorkSettingDao.add(homeWorkSetting);
                    return JsonResult.success("添加成功");
                } else {
                    //更新
                    homeWorkSettingDao.update(homeWorkSetting);
                    return JsonResult.success("更新成功");
                }
            }
        }
        return JsonResult.error();
    }

    @Override
    public JsonResult del(String id) {
        try {
            homeWorkSettingDao.del(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }
}
